<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Register extends CI_Controller {
// 
//  register.php
//  phpweb
//  
//  Created by WillLee on 2011-06-28.
//  Copyright 2011 lishuzu. All rights reserved.
// 
	function __construct()
	{
		parent::__construct();
		$this->load->model('user_model');
		$this->load->model('invite_model');
	}
	
	public function index(){

	}
	public function invite($email = '', $invitecode = '')
	{
		
		$data = array();
		if (empty($email) || empty($invitecode)) {
			showmessage('对不起您的链接不正确，建议让你的朋友再次邀请你一次', 'index', 2);
		}
		$data['get_email'] = $email;
		$data['email'] = xss_clean(urldecode($email));
		$data['invitecode'] = xss_clean($invitecode);
		$data['usernamemsg'] = $this->user_model->getuser('username', $data['email']) ? '该email已经被注册':'';
		$data['invitecodemsg'] = $this->invite_model->checkcode($data['email'],$invitecode) ? '':'邀请码失效';
		if (submitcheck('invitesubmit')) {
			if (!empty($data['usernamemsg']) || !empty($data['invitecodemsg'])) {
				showmessage('用户名已经注册或邀请码失效，', 'index', 3);
			}
			
			$username = $data['email'];
			$name = $this->input->post('name', true);
			$password = $this->input->post('password', true);
			$repassword = $this->input->post('repassword', true);
			$intro = $this->input->post('intro', true);
			if ($password !== $repassword) {
				showmessage('确认密码和密码不相同');
			}
			//密码随机hash
			$salt = substr(uniqid(rand()), -6);
			$password = md5(md5($password).$salt);
			//普通会员
			$gid = 3;
			$usergroup = $this->config->item('usergroup');
			
			$insertdata = array(
				'username' => trim($username),
				'name' => trim($name),
				'password' => $password,
				'intro' => $intro,
				'salt' => $salt,
				'dateline' => now(),
				'updatetime' => now(),
				'ip' => getonlineip(),
				'groupid' => $gid,
				'mailnotice' => '1',//邮件提醒
				'invitetime ' => $usergroup[$gid]['invitetime'],
				);

			//取消html
			$insertdata = shtmlspecialchars($insertdata);

			foreach ($insertdata as $key => $value) {
				if (!in_array($key, array('ip'))) {
					if (empty($value)) {
						showmessage('请确保表单信息完整');
					}					
				}
			}
			
			$uid = $this->user_model->add($insertdata);
			if ($uid) {
				// 失效邀请码
				$this->invite_model->delete($data['email'],$invitecode);
				// 登录
				$userinfo = $this->user_model->getuser('uid', $uid);
				$session_data = array(
					'user' => $userinfo
					);
				$this->session->set_userdata($session_data);
				showmessage('注册成功，欢迎来到T大爷社区', 'home/index', 2);
			}
		}
		$this->load->template('invite_register_tpl',$data);
	}
}

/* End of file index.php */
/* Location: ./application/controllers/index.php */